package com.pfsoft.config;/**
 * @author PF
 * Created by dell on 2018-05-27.
 */

import com.pfsoft.dto.SysUserRespository;
import com.pfsoft.entity.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.cas.authentication.CasAssertionAuthenticationToken;
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * com.pfsoft.config
 *
 * @author PF
 * @create 2018-05-27 9:48
 **/
@Component
public class AcmCasUserDetailService implements AuthenticationUserDetailsService<CasAssertionAuthenticationToken> {
    private static final Logger USER_SERVICE_LOGGER = LoggerFactory.getLogger(AcmCasUserDetailService.class);

    @Resource
    private SysUserRespository sysUserRespository;


    @Override
    public UserDetails loadUserDetails(CasAssertionAuthenticationToken token) throws UsernameNotFoundException {
        USER_SERVICE_LOGGER.info("校验成功的登录名为: " + token.getName());
        //此处涉及到数据库操作然后读取权限集合，读者可自行实现
        SysUser sysUser = sysUserRespository.findByUserName(token.getName());
        if (null == sysUser) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        return sysUser;
    }

}
